package com.xiaozhou.leetCode.leetCode800;

public class LeetCode844 {


    public static boolean backspaceCompare(String s, String t) {
        int s1 = s.length() - 1;
        int t1 = t.length() - 1;
        int skpS = 0;
        int skpT = 0;
        char sC = 0;
        char tC = 0;
        while (s1 >= 0 || t1 >= 0){
            if (s1 >= 0){
                if (s.charAt(s1) != '#'){
                    if (skpS == 0){
                        sC = s.charAt(s1);
                    }else {
                        skpS--;
                    }
                }else {
                    skpS++;
                }
            }

            if (t1 >= 0){
                if (t.charAt(t1) != '#'){
                    if (skpT == 0){
                        tC = t.charAt(t1);
                    }else {
                        skpT--;
                    }
                }else {
                    skpT++;
                }
            }

            if ((sC != 0 && tC != 0) || s1 < 0 || t1 < 0){
                if (sC != tC){
                    return false;
                }else {
                    sC = 0;
                    tC = 0;
                }
            }
            if (sC == 0 || s.charAt(s1) == '#'){
                s1--;
            }
           if (tC == 0 || t.charAt(t1) == '#'){
               t1--;
           }
        }
        return sC == tC;
    }

    public static void main(String[] args) {
        boolean compare = backspaceCompare("xywrrmp", "xywrrmu#p");
        boolean compare1 = backspaceCompare("bxj###tw", "bxj##tw");
        boolean compare2 = backspaceCompare("a", "aa#a");
        System.out.println("compare: " + compare);
        System.out.println("compare1: " + compare1);
        System.out.println("compare2: " + compare2);
    }
}
